//
//  STNativeAdmobView.swift
//  iPhone
//
//  Created by Toj on 5/30/23.
//

import UIKit
import GoogleMobileAds

class STNativeAdmobView: GADNativeAdView {
    
    private let random = arc4random_uniform(2)
    
    private var isLeftRight: Bool {
        return random == 0
    }
    
    override var nativeAd: GADNativeAd? { didSet {
        super.nativeAd = nativeAd
        guard let native = nativeAd else { return }
        titleLabel.text = native.headline
        bodyLabel.text = native.body
        
        /// 媒体资源
        let mediaContent = native.mediaContent
        mediumView.mediaContent = mediaContent

        // 小星星数量 (0 to 5).
        let starRating = native.starRating ?? 0
        print("-->toj: starRating: \(starRating)")
        // 安装文案
        let callToAction = native.callToAction ?? ""
        print("-->toj: callToAction: \(callToAction)")
        // 安装去向
        let store = native.store ?? ""
        print("-->toj: store: \(store)")
        // 价钱
        let price = native.price ?? ""
        print("-->toj: price: \(price)")
        // 广告商
        let advertiser = native.advertiser ?? ""
        print("-->toj: advertiser: \(advertiser)")
        // 广告图标
        // let adIcon = native.icon?.image
    } }
    
    /* iconView */
    private(set) lazy var iconImageView = UIImageView()
    
    /* iconView */
    private(set) lazy var iconImageView2 = UIImageView()
    
    /// 媒体资源
    private(set) lazy var mediumView: GADMediaView = {
        let view = GADMediaView()
        return view
    }()
    
    /* adFlagLabel */
    private lazy var adFlagLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .center
        view.backgroundColor = "#FFBE19".to_color
        view.textColor = .white
        view.font = 14.to_boldFont
        view.text = "AD"
        return view
    }()
    
    /* titleLabel */
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.font = 14.to_boldFont
        return view
    }()
    
    /* bodyLabel */
    private lazy var bodyLabel: UILabel = {
        let view = UILabel()
        view.font = 12.to_boldFont
        view.numberOfLines = 0
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print("-->toj: STNativeAdmobView - deinit")
    }
}

// MARK: - 初始化
extension STNativeAdmobView {
    
    private func setupMakeAddSubviews() {
        // 点击事件基于内部控件上的, 所以给内部控件赋值才可以点击跳转
        mediaView = mediumView
        headlineView = titleLabel
        bodyView = bodyLabel
        
        addSubview(mediumView)
        addSubview(titleLabel)
        addSubview(bodyLabel)
        addSubview(adFlagLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        let height = adFlagLabel.cm_stringOneLineHeight
        let width = adFlagLabel.cm_stringOneLineWidth
        adFlagLabel.snp.remakeConstraints { make in
            make.top.right.equalTo(self).inset(3)
            make.height.equalTo(height + 2)
            make.width.equalTo(width + 5)
        }
        
        guard isLeftRight else {
            mediumView.snp.makeConstraints { make in
                make.left.right.top.equalTo(self)
                make.height.equalTo(189)
            }
            
            titleLabel.snp.makeConstraints { make in
                make.left.right.bottom.equalTo(self)
                make.top.equalTo(mediumView.snp.bottom)
            }
            return
        }
        
        mediumView.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(100)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(mediumView)
            make.left.equalTo(mediumView.snp.right)
            make.right.equalTo(self)
        }
        
        bodyLabel.snp.makeConstraints { make in
            make.bottom.equalTo(mediumView)
            make.left.equalTo(mediumView.snp.right)
            make.right.equalTo(self)
        }
    }
}
